# 批量替换@Slf4j注解为手动log声明的PowerShell脚本

# 获取所有Java文件
$javaFiles = Get-ChildItem -Path "src\main\java" -Filter "*.java" -Recurse

foreach ($file in $javaFiles) {
    Write-Host "处理文件: $($file.FullName)"
    
    # 读取文件内容
    $content = Get-Content $file.FullName -Raw -Encoding UTF8
    
    # 检查是否包含@Slf4j注解
    if ($content -match '@Slf4j') {
        Write-Host "  发现@Slf4j注解，开始替换..."
        
        # 移除@Slf4j注解
        $content = $content -replace '@Slf4j\s*\r?\n', ''
        
        # 检查是否已经有Logger导入
        $hasLoggerImport = $content -match 'import org\.slf4j\.Logger;'
        $hasLoggerFactoryImport = $content -match 'import org\.slf4j\.LoggerFactory;'
        
        # 如果没有Logger导入，添加导入语句
        if (-not $hasLoggerImport -or -not $hasLoggerFactoryImport) {
            # 找到package声明后的位置
            if ($content -match '(package [^;]+;\s*\r?\n)') {
                $packageDeclaration = $matches[1]
                $replacement = $packageDeclaration + "`r`n"
                
                if (-not $hasLoggerImport) {
                    $replacement += "import org.slf4j.Logger;`r`n"
                }
                if (-not $hasLoggerFactoryImport) {
                    $replacement += "import org.slf4j.LoggerFactory;`r`n"
                }
                
                $content = $content -replace [regex]::Escape($packageDeclaration), $replacement
            }
        }
        
        # 检查是否已经有log变量声明
        if (-not ($content -match 'private static final Logger log')) {
            # 找到类声明的位置，在类开始后添加log声明
            if ($content -match '(public class [^{]+\{\s*\r?\n)') {
                $classDeclaration = $matches[1]
                $className = $file.BaseName
                $replacement = $classDeclaration + "    private static final Logger log = LoggerFactory.getLogger($className.class);`r`n`r`n"
                $content = $content -replace [regex]::Escape($classDeclaration), $replacement
            }
        }
        
        # 写回文件
        $content | Set-Content $file.FullName -Encoding UTF8
        Write-Host "  处理完成"
    }
}

Write-Host "所有文件处理完成！"
Write-Host "请运行 mvn compile 检查编译是否成功。"